package org.kyw.pet.service.impl;

import org.kyw.basic.service.impl.BaseServiceImpl;
import org.kyw.basic.util.CodeGenerateUtils;
import org.kyw.basic.util.DistanceUtil;
import org.kyw.basic.util.PageList;
import org.kyw.basic.util.Point;
import org.kyw.order.domain.PetAcquisitionOrder;
import org.kyw.order.mapper.PetAcquisitionOrderMapper;
import org.kyw.org.domain.Employee;
import org.kyw.org.domain.Shop;
import org.kyw.org.mapper.EmployeeMapper;
import org.kyw.org.mapper.ShopMapper;
import org.kyw.pet.domain.Pet;
import org.kyw.pet.domain.SearchMasterMsg;
import org.kyw.pet.mapper.SearchMasterMsgMapper;
import org.kyw.pet.query.SearchMasterMsgQuery;
import org.kyw.pet.service.IPetService;
import org.kyw.pet.service.ISearchMasterMsgService;
import org.kyw.user.domain.Logininfo;
import org.kyw.user.domain.User;
import org.kyw.user.mapper.UserMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

@Service
public class SearchMasterMsgServiceImpl extends BaseServiceImpl<SearchMasterMsg> implements ISearchMasterMsgService {
    @Autowired
    private SearchMasterMsgMapper searchMasterMsgMapper;
    @Autowired
    private UserMapper userMapper;
    @Autowired
    private ShopMapper shopMapper;
    @Autowired
    private EmployeeMapper employeeMapper;
    @Autowired
    private IPetService petService;
    @Autowired
    private PetAcquisitionOrderMapper petAcquisitionOrderMapper;

    @Override
    public void publish(SearchMasterMsg searchMasterMsg, Logininfo logininfo) {
        //关联当前登录人
        User user = userMapper.loadByLogininfoId(logininfo.getId());
        searchMasterMsg.setUser_id(user.getId());
        //关联最近的店铺
        Point userAddress = DistanceUtil.getPoint(searchMasterMsg.getAddress());
        Shop nearestShop = DistanceUtil.getNearestShop(userAddress, shopMapper.loadAll());
        searchMasterMsg.setShop_id(nearestShop.getId());

        searchMasterMsgMapper.save(searchMasterMsg);
    }

    @Override
    public PageList<SearchMasterMsg> list(SearchMasterMsgQuery msgQuery, Logininfo currentLogininfo) {
        if(StringUtils.isEmpty(msgQuery.getState())){
            User user = userMapper.loadByLogininfoId(currentLogininfo.getId());
            msgQuery.setUserId(user.getId());
        }
        Employee employee = employeeMapper.loadByLogininfoId(currentLogininfo.getId());
        msgQuery.setShopId(employee.getShop_id());
        return super.queryPage(msgQuery);
    }

    @Override
    public void handle(Pet pet, Logininfo currentLogin) {
        //修改寻主消息状态
        searchMasterMsgMapper.updateState2ProccedById(pet.getSearch_master_msg_id());
        //添加宠物信息
        petService.add(pet);
        //收购信息
        SearchMasterMsg adopt = searchMasterMsgMapper.loadById(pet.getSearch_master_msg_id());
        Employee employee = employeeMapper.loadByLogininfoId(currentLogin.getId());
        PetAcquisitionOrder order = pet2order(pet, adopt, employee.getId());
        petAcquisitionOrderMapper.save(order);
    }

    private PetAcquisitionOrder pet2order(Pet pet, SearchMasterMsg adopt, Long empId) {
        PetAcquisitionOrder order = new PetAcquisitionOrder();
        order.setDigest("[摘要]对"+pet.getName()+"收购订单！");
        order.setState(0);//待支付
        order.setPrice(pet.getCostprice());
        order.setAddress(adopt.getAddress());
        String orderSn = CodeGenerateUtils.generateOrderSn(adopt.getUser_id());
        order.setOrderSn(orderSn);
        order.setPet_id(pet.getId());
        order.setUser_id(adopt.getUser_id());
        order.setPaytype(pet.getPeyType());
        order.setShop_id(pet.getShop_id());
        order.setEmp_id(empId);
        return order;
    }
}
